﻿// Copyright (c) 2009 John Ehn, ExtremeSwank.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using ExtremeSwank.OpenId.PlugIns.Extensions;

namespace ExtremeSwank.OpenId
{
    /// <summary>
    /// Provides a simple registry of OpenID Extension namespaces to
    /// the supporting IExtension type.
    /// </summary>
    /// <remarks>Facilitates auto-loading extensions based on data
    /// received from Identity Providers.</remarks>
    public static class ExtensionRegistry
    {
        static Dictionary<string, Type> _Registry;

        /// <summary>
        /// Look up a plugin's Type using its namespace URI.
        /// </summary>
        /// <param name="name">The namespace URI to match.</param>
        /// <returns>The Type matching the namespace URI.</returns>
        public static Type Get(string name) 
        {
            if (String.IsNullOrEmpty(name)) { return null; }
            if (_Registry == null) { _Registry = new Dictionary<string, Type>(); Fill(); }
            if (_Registry.ContainsKey(name)) { return _Registry[name]; }
            return null;
        }

        /// <summary>
        /// Add a plugin's Type to the registry. 
        /// </summary>
        /// <remarks>The registry is in-memory, and will only remain in the current processing context.
        /// By default, ExtensionRegistry automatically registers all IExtension
        /// Types shipped with the library.  If you are registering custom
        /// IExtension Types, ensure you register the Type upon each page load.</remarks>
        /// <param name="name">The namespace URI to register</param>
        /// <param name="type">The Type to register</param>
        public static void Set(string name, Type type)
        {
            if (_Registry == null) { _Registry = new Dictionary<string, Type>(); Fill(); }
            _Registry[name] = type;
        }

        static void Fill()
        {
            _Registry[ProtocolUri.AttributeExchange1Dot0.AbsoluteUri] = typeof(AttributeExchange);
            _Registry[ProtocolUri.SimpleRegistration1Dot1.AbsoluteUri] = typeof(SimpleRegistration);
            _Registry[ProtocolUri.AuthenticationPolicy1Dot0.AbsoluteUri] = typeof(AuthenticationPolicy);
        }

    }
}
